package cn.itcast.foodapp2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import cn.itcast.foodapp2.bean.CommentsListBean;
import cn.itcast.foodapp2.bean.FindOneBean;
import cn.itcast.foodapp2.bean.FoodListBean;
import cn.itcast.foodapp2.net.NetUtil;

public class CommentsListActivity extends AppCompatActivity {
    private static final String TAG = "CommentsListActivity";

    int id = 0;
    String foodName;
    MyAdapter myAdapter;
    private List<CommentsListBean.DataDTO> list;
    TextView mTvFoodName;
    ListView mLvComments;

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list != null ? list.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final CommentsListBean.DataDTO dataBean = list.get(position);
            View view = View.inflate(getApplicationContext(),
                    R.layout.comments_list_item, null);
            TextView tvMessage = view.findViewById(R.id.tv_comments_message);
            tvMessage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int id = dataBean.getId();//get current food id
                    Intent intent = new Intent(CommentsListActivity.this, CommentsDetailActivity.class);
                    intent.putExtra("id", id);//add foodId into intent
                    intent.putExtra("foodName", mTvFoodName.getText().toString() + "");
                    startActivity(intent);//
                }
            });
            tvMessage.setText(dataBean.getComment());
            return view;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comments_list);
        id = getIntent().getIntExtra("id", 1);
        foodName = getIntent().getStringExtra("foodName");
        initView();
        initData();
        initEvent();
    }

    private void initEvent() {

    }

    private void initData() {
        mTvFoodName.setText(foodName + "");
        new Thread(new Runnable() {

            @Override
            public void run() {
                String url = "http://42.48.39.55:8081/foodserver/comments/list/" + id;
                String data = NetUtil.getData(new JSONObject(), url);
                final CommentsListBean commentsListBean = new Gson().fromJson(data, CommentsListBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        list = commentsListBean.getData();
                        myAdapter = new MyAdapter();
                        mLvComments.setAdapter(myAdapter);
                    }
                });
            }
        }).start();
    }

    private void initView() {
        mTvFoodName = findViewById(R.id.tv_food_name_comments_list);
        mLvComments = findViewById(R.id.lv_list_comments);

    }
}